package config

import (
	config "ac-common-go/config"
	log "ac-common-go/glog"
	"ac-julink/service"
	"ac-julink/socket"
	"reflect"
)

var ConfigInfo *Config

type Config struct {
	Mymap  map[string]interface{}
	strcet string
}

func (this *Config) Load() error {
	this.Mymap = make(map[string]interface{})
	conf, err := config.ReadConfigFile("./conf/blacklist/blacklist_config.conf")
	if err != nil {
		log.Errorf("read config file failed:err[%v]", err)
		return err
	}
	defer this.Print()

	// section self
	this.Mymap["FeatureCodeBlackList"], err = conf.GetString("blacklist", "featureCodeList")
	if err != nil || len(this.Mymap["FeatureCodeBlackList"].(string)) <= 0 {
		// set default value as empty string
		this.Mymap["FeatureCodeBlackList"] = ""
	}

	this.Mymap["WifiIdBlackList"], err = conf.GetString("blacklist", "wifiIdList")
	if err != nil || len(this.Mymap["WifiIdBlackList"].(string)) <= 0 {
		this.Mymap["WifiIdBlackList"] = ""
	}

	// the interval not calling wifiOnline() for 902 after 101(millSecond).
	this.Mymap["HbToPsMillThreshold"], err = conf.GetInt64("common", "hbToPsMillThreshold")
	if err != nil {
		this.Mymap["HbToPsMillThreshold"] = 60000
	}

	log.Warningf("read blacklist from ./conf/blacklist/blacklist_config.conf -> FeatureCodeBlackList : %s, WifiIdBlackList: %s, HbToPsMillThreshold : %d", this.Mymap["FeatureCodeBlackList"].(string), this.Mymap["WifiIdBlackList"].(string), this.Mymap["HbToPsMillThreshold"].(int64))

	socket.BlackListConf.SetConfigs(
		this.Mymap["FeatureCodeBlackList"].(string),
		this.Mymap["WifiIdBlackList"].(string),
	)
	service.HbConf.SetConfigs(this.Mymap["HbToPsMillThreshold"].(int64))

	return nil
}
func (this *Config) Print() {
	log.Infoln("start print the virtual current config")
	this.printConfigItem("blacklist", "featureCodeList", this.Mymap["FeatureCodeBlackList"])
	this.printConfigItem("blacklist", "wifiIdList", this.Mymap["WifiIdBlackList"])

	log.Infoln("finish print the virtual current config")
}

func (this *Config) printConfigItem(section, name string, item interface{}) {
	log.Infof("config item info[%s.%s.(%v)]", section, name, reflect.TypeOf(item))
}
func Init() {
	defer func() {
		if err := recover(); err != nil {
			log.Errorf("blacklist init failed: %v", err)
		}
	}()
	myConfig := new(Config)
	myConfig.Load()
	ConfigInfo = myConfig
}
func GetConfigValue(key string) string {
	defer func() {
		if err := recover(); err != nil {
			log.Errorf("get config value error: %s, %+v", key, err)
		}
	}()
	v, found := ConfigInfo.Mymap[key].(string)
	if !found {
		return ""
	}
	return v
}

func GetConfigInt64Value(key string) int64 {
	defer func() {
		if err := recover(); err != nil {
			log.Errorf("get config value error: %s, %+v", key, err)
		}
	}()
	v, found := ConfigInfo.Mymap[key].(int64)
	if !found {
		return 0
	}
	return v
}
